/*
 * @Author: Otway
 * @Date: 2019-01-09 11:16:08
 * @LastEditors: Otway
 * @LastEditTime: 2019-08-30 18:50:09
 * @copyright: h-visions
 */
import React, { PureComponent } from 'react';
import { openModalMessage } from '../../../components/TableCustomize'
import * as CheckManagementService from '../../../axios/CheckManagement'
import * as APIServiceYQ from '../../../axios/api_yq'
import { typeList } from '../../../utils/global'
import {
    Row,
    Col,
    Form,
    Select,
    Input,
    Alert,
    Spin
} from 'antd';
const FormItem = Form.Item;
const { Option } = Select

class AddFrom extends PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            userId: localStorage.getItem('userId'),
            batchNoAndCodes: [],
            inspectionOrderNumber: '',
            list: [],
            dataSource: [],
            code: '',
            inspectionFormType: '',
        }
    }
    componentDidMount() {
        this.getList()
    }

    getList = () => {
        const { userId } = this.state
        CheckManagementService.getWorkOrderCheckInfo(userId).then(res => {
            if (res.data.code != 200) {
                openModalMessage('warning', res.data.msg)
                return
            }
            let data = res.data.data
            this.setState({
                batchNoAndCodes: data.batchNoAndCodes,
                inspectionOrderNumber: data.inspectionOrderNumber
            })
        })
        APIServiceYQ.queryQualityForm({ pageNum: 1, pageSize: 999999 }).then(res => {
            if (res.data.code != 200) {
                openModalMessage('warning', res.data.msg)
                return
            }
            console.log(res, 'queryQualityForm');
            let list = res.data.data.data
            this.setState({ list })
        })
    }
    // 选择批号
    handleSelect = (record) => {
        console.log(record, 'handleSelect');
        if (record) {
            let key = ''
            let obj = this.state.batchNoAndCodes.find(item => {
                key = Object.keys(item)[0]
                return key == record
            })
            this.props.form.setFieldsValue({ code: obj[key] })
            let dataSource = [], code = obj[key], inspectionFormType = this.state.inspectionFormType
            console.log(code, 'code', inspectionFormType, ' inspectionFormType');
            this.state.list.map(item => {
                let arr = item.object.split('、')
                if (inspectionFormType) {
                    if (arr.indexOf(code) != -1 && inspectionFormType == item.type) {
                        dataSource.push(item)
                    }
                } else {
                    if (arr.indexOf(code) != -1) {
                        dataSource.push(item)
                    }
                }
            })
            console.log(this.state.list, dataSource, 'dataSource');
            this.setState({
                code: obj[key], dataSource
            })
        } else {
            this.props.form.setFieldsValue({ code: null })
            this.setState({ code: null, dataSource: [] })
        }
    }
    // 选择检验表单类型
    handleSelect1 = (record) => {
        console.log(record, 'handleSelect1');
        let dataSource = [], code = this.state.code, inspectionFormType = record
        if (record) {
            console.log(code, 'code', inspectionFormType, ' inspectionFormType');
            this.state.list.map(item => {
                let arr = item.object.split('、')
                if (inspectionFormType) {
                    if (arr.indexOf(code) != -1 && inspectionFormType == item.type) {
                        dataSource.push(item)
                    }
                } else {
                    if (arr.indexOf(code) != -1) {
                        dataSource.push(item)
                    }
                }
            })
        }
        this.setState({ dataSource, inspectionFormType: record })
        this.props.form.setFieldsValue({ inspectionFormName: undefined, inspectionFormCode: undefined })
    }
    // handleSelect2 = (record, 'name', 'code', 'inspectionFormName')
    // 选择检验表单名称/编码
    handleSelect2 = (record, type1, type2, type3) => {
        console.log(record, 'handleSelect1');
        if (record) {
            let obj = this.state.dataSource.find(item => {
                return item[type1] == record
            })
            this.props.form.setFieldsValue({ [type3]: obj[type2] })
        } else {
            this.props.form.setFieldsValue({ [type3]: undefined })
        }
    }


    render() {
        const { form: { getFieldDecorator }, selectList1 } = this.props;
        const { batchNoAndCodes, inspectionOrderNumber, dataSource } = this.state
        const formItemLayout = {
            // 抽屉里面Form。二列的布局比例
            labelCol: {
                span: 8
            },
            wrapperCol: {
                span: 12
            }
        };
        return (
            <Form >
                <Row>
                    <Col span={24} hidden>
                        <FormItem {...formItemLayout} label={''}>
                            {getFieldDecorator('inspectionOrderNumber', {
                                initialValue: inspectionOrderNumber
                            })(<Input allowClear disabled />)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'创建人'}>
                            {getFieldDecorator('name', {
                                initialValue: localStorage.getItem('userName')
                            })(<Input allowClear disabled />)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'批号'}>
                            {getFieldDecorator('batchNo', {
                                rules: [
                                    {
                                        required: true,
                                        message: '请选择'
                                    }
                                ]
                            })(<Select placeholder='请选择' allowClear style={{ width: '100%' }} showSearch
                                filterOption={(input, option) =>
                                    option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                                }
                                onChange={this.handleSelect}
                            >
                                {
                                    batchNoAndCodes.map(item => {
                                        return <Option key={Object.keys(item)[0]}>{Object.keys(item)[0]}</Option>
                                    })
                                }
                            </Select>)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'机型'}>
                            {getFieldDecorator('code')(<Input placeholder='请输入' allowClear disabled />)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'检验表单类型'}>
                            {getFieldDecorator('inspectionFormType', {
                                rules: [
                                    {
                                        required: true,
                                        message: '请选择'
                                    }
                                ]
                            })(<Select placeholder='请选择' allowClear style={{ width: '100%' }} showSearch
                                filterOption={(input, option) =>
                                    option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                                }
                                onChange={this.handleSelect1}
                            >
                                {
                                    selectList1.map(item => {
                                        return <Option key={item.key}>{item.label}</Option>
                                    })
                                }
                            </Select>)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'检验表单名称'}>
                            {getFieldDecorator('inspectionFormName', {
                                rules: [
                                    {
                                        required: true,
                                        message: '请选择'
                                    }
                                ]
                            })(<Select placeholder='请选择' allowClear style={{ width: '100%' }} showSearch
                                filterOption={(input, option) =>
                                    option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                                }
                                onChange={(record) => this.handleSelect2(record, 'name', 'code', 'inspectionFormCode')}
                            >
                                {
                                    dataSource.map(item => {
                                        return <Option key={item.name}>{item.name}</Option>
                                    })
                                }
                            </Select>)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'检验表单编码'}>
                            {getFieldDecorator('inspectionFormCode', {
                                rules: [
                                    {
                                        required: true,
                                        message: '请选择'
                                    }
                                ]
                            })(<Select placeholder='请选择' allowClear style={{ width: '100%' }} showSearch
                                filterOption={(input, option) =>
                                    option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                                }
                                onChange={(record) => this.handleSelect2(record, 'code', 'name', 'inspectionFormName')}
                            >
                                {
                                    dataSource.map(item => {
                                        return <Option key={item.code}>{item.code}</Option>
                                    })
                                }
                            </Select>)}
                        </FormItem>
                    </Col>
                </Row>
            </Form>
        );
    }
}

export default Form.create()(AddFrom);
